import type { PageResponse } from './warehouse';

/**
 * 库存管理相关API
 */
import type { InventoryApi } from '#/models/inventory/inventory';

import { requestClient } from '#/api/request';

/**
 * 库存调整参数接口
 */
export interface InventoryAdjustParams {
  id: number | string;
  adjust_quantity: number;
  reason: string;
  remark?: string;
}

/**
 * 库存移动参数接口
 */
export interface InventoryMoveParams {
  id: number | string;
  from_warehouse_id: number | string;
  from_location_id: number | string;
  to_warehouse_id: number | string;
  to_location_id: number | string;
  move_quantity: number;
  reason: string;
  remark?: string;
}

/**
 * 库存盘点参数接口
 */
export interface InventoryCheckParams {
  id: number | string;
  actual_quantity: number;
  remark?: string;
}

/**
 * 库存列表查询参数接口
 */
export interface InventoryQueryParams {
  page?: number;
  pageSize?: number;
  product_id?: number;
  warehouse_id?: number;
  location_id?: number;
  batch_number?: string;
  status?: string;
  [key: string]: any;
}

/**
 * 获取库存列表
 * @param params - 查询参数，支持分页、过滤和排序
 * @returns 分页的库存列表数据
 */
export const getInventoryList = (
  params?: InventoryQueryParams,
): Promise<PageResponse<InventoryApi.Inventory>> => {
  return requestClient.get('/inventory/inventory/', { params });
};

/**
 * 获取单个库存详情
 * @param inventoryId - 库存ID
 * @returns 库存详情
 */
export const getInventory = (
  inventoryId: number,
): Promise<InventoryApi.Inventory> =>
  requestClient.get(`/inventory/inventory/${inventoryId}/`);

/**
 * 创建库存记录
 * @param data - 库存数据
 * @returns 创建成功的库存信息
 */
export const createInventory = (
  data: Partial<InventoryApi.Inventory>,
): Promise<InventoryApi.Inventory> =>
  requestClient.post('/inventory/inventory/', data);

/**
 * 更新库存记录
 * @param inventoryId - 库存ID
 * @param data - 更新的库存信息
 * @returns 更新后的库存信息
 */
export const updateInventory = (
  inventoryId: number,
  data: Partial<InventoryApi.Inventory>,
): Promise<InventoryApi.Inventory> =>
  requestClient.put(`/inventory/inventory/${inventoryId}/`, data);

/**
 * 删除库存记录
 * @param inventoryId - 库存ID
 * @returns 删除结果
 */
export const deleteInventory = (inventoryId: number): Promise<any> => {
  return requestClient.delete(`/inventory/inventory/${inventoryId}/`);
};

/**
 * 调整库存数量
 * @param params - 调整参数
 * @returns 调整结果
 */
export const adjustInventory = (
  params: InventoryAdjustParams,
): Promise<InventoryApi.Inventory> => {
  return requestClient.post('/inventory/inventory/adjust/', params);
};

/**
 * 移动库存
 * @param params - 移动参数
 * @returns 移动结果
 */
export const moveInventory = (
  params: InventoryMoveParams,
): Promise<InventoryApi.Inventory> => {
  return requestClient.post('/inventory/inventory/move/', params);
};

/**
 * 库存盘点
 * @param params - 盘点参数
 * @returns 盘点结果
 */
export const checkInventory = (
  params: InventoryCheckParams,
): Promise<InventoryApi.Inventory> => {
  return requestClient.post('/inventory/inventory/check/', params);
};

/**
 * 导出库存数据
 * @param params - 导出参数
 * @returns 导出结果
 */
export const exportInventoryData = (
  params?: InventoryQueryParams,
): Promise<Blob> => {
  return requestClient.get('/inventory/inventory/export/', {
    params,
    responseType: 'blob',
  });
};

/**
 * 获取库存移动记录
 * @param params - 查询参数
 * @returns 库存移动记录列表
 */
export const getInventoryMovements = (params?: {
  inventory_id?: number | string;
  page?: number;
  pageSize?: number;
}): Promise<PageResponse<any>> => {
  return requestClient.get('/inventory/inventory/movements/', { params });
};

/**
 * 获取库存汇总信息
 * @param params - 查询参数
 * @returns 库存汇总数据
 */
export const getInventorySummary = (params?: any): Promise<any> => {
  return requestClient.get('/inventory/inventory/get_inventory_summary/', {
    params,
  });
};

/**
 * 获取仓库列表（简化版）
 * @returns 仓库列表
 */
export const getWarehouseListSimple = (): Promise<any[]> => {
  return requestClient.get('/inventory/warehouse/');
};
